package com.onpositive.wiki3.db.catmodel.matches.classify;

import java.util.LinkedHashSet;

import com.onpositive.nlp.sequence.ClassifiedSequence;
import com.onpositive.nlp.sequence.ClassifiedSequence.Token;
import com.onpositive.nlp.sequence.SequenceParser;
import com.onpositive.wiki3.db.catmodel.Categories;
import com.onpositive.wiki3.db.catmodel.Category;
import com.onpositive.wiki3.db.catmodel.ICategoryMatcher;
import com.onpositive.wiki3.db.impl.WikiDBImpl;

public final class PluralCoreMatcher implements ICategoryMatcher {
	private final WikiDBImpl wikiDBImpl;
	private final Categories c;
	private final RelationStore ss;

	public PluralCoreMatcher(WikiDBImpl wikiDBImpl, Categories c,
			RelationStore ss) {
		this.wikiDBImpl = wikiDBImpl;
		this.c = c;
		this.ss = ss;
	}
	
	

	@Override
	public int accept(Category st) {

		ClassifiedSequence parse = SequenceParser.parse(st.toString(),
				wikiDBImpl);
		for (Token s : parse.tokens()) {
			if (s.name.equals("by")) {
				// Category[] childCategories = st.getChildCategories();
				// for (Category c : childCategories) {
				// System.out.println(st + "->" + c);
				// ss.recordRelation(RelationConstants.IS_A, st, c);
				// }
				return 0;
				// }
			}
		}
		Token pluralCore = parse.getCore();
		Category[] parentsCategories = st.getParentsCategories();
		parentsCategories = st.getChildCategories();
		LinkedHashSet<Token> linkedHashSet = new LinkedHashSet<Token>(
				parse.tokens());
		linkedHashSet.remove(pluralCore);
		// System.out.println(pluralCore);
		if (pluralCore == null) {
			return 0;
		}
		String name = pluralCore.name;
		for (Category c : parentsCategories) {
			ClassifiedSequence parse2 = SequenceParser.parse(c.toString(),
					wikiDBImpl);
			Token pluralCore2 = parse2.getCore();
			if (pluralCore2 != null) {
				// if (//pluralCore.name.co)
				if (pluralCore2.name.equals("types")
						|| pluralCore2.name.equals("classes")) {

					ss.recordRelation(RelationConstants.IS_A, st, c);
					continue;
				}
				if (pluralCore2.name.contains(name)) {
					ss.recordRelation(RelationConstants.IS_A, st, c);
				}
			}
		}
		return 0;
	}
}